#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_mixer.h>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <string.h>
#include "perso.h"

int  saisie (SDL_Surface *ecran,SDL_Event event, int *pointeur,int *maxW, int *maxH)
{
	static int var=0;
	int ii=0,choix_personnage=0;
	char lettre [15]= "";
	char caractereActuel;
	int longeur=0; 
	int etat_back=0;
	static int etat_start =0;
	int etat_start1=0;
	
	SDL_Surface *a = NULL, *b = NULL, *c = NULL,*d = NULL,*e = NULL,*f = NULL, *g = NULL, *h = NULL,*i = NULL, *j = NULL, *k = NULL, *l = NULL,*m = NULL,*n= NULL,*o = NULL,*p = NULL,*q = NULL, *nom = NULL, *back = NULL;
	SDL_Surface *r = NULL, *s = NULL, *t = NULL,*u = NULL,*v = NULL,*w = NULL,*x = NULL,*y = NULL,*z = NULL,*aa = NULL,*bb = NULL,*cc = NULL, *dd = NULL, *fenetre = NULL, *start = NULL, *startClic = NULL, *backClic = NULL;

	SDL_Rect position_a, position_b, position_c , position_j,position_m ,position_n ,position_o ,position_p ,position_q, position_fenetre, position_start ;
	SDL_Rect position_u ,position_v ,position_w ,position_x ,position_y ,position_z ,position_aa ,position_bb ,position_cc, position_dd, position_nom ;
	SDL_Rect position_r , position_s , position_t ,position_d ,position_e ,position_f ,position_i , position_k , position_l, position_h, position_g, position_back ;
	 // controle de clavier virtuel

	position_y.x =*maxW /2;
	position_y.y =*maxH / 1.4;

	position_a.x =position_y.x-180;
	position_a.y =*maxH / 1.4;

	position_z.x =position_y.x-144;
	position_z.y =*maxH / 1.4;

	position_e.x =position_y.x -108;
	position_e.y =*maxH / 1.4;

	position_r.x =position_y.x -72;
	position_r.y =*maxH / 1.4;

	position_t.x =position_y.x-36;
	position_t.y =*maxH / 1.4;

	position_u.x =position_y.x+36;
	position_u.y =*maxH / 1.4;

	position_i.x =position_y.x+72;
	position_i.y =*maxH / 1.4;

	position_o.x =position_y.x+108;
	position_o.y =*maxH / 1.4;

	position_p.x =position_y.x+144;
	position_p.y =*maxH / 1.4;

	position_q.x =position_a.x +15;
	position_q.y =position_y.y +40;

	position_s.x =position_z.x +15;
	position_s.y =position_y.y +40;

	position_d.x =position_e.x +15;
	position_d.y =position_y.y +40;

	position_f.x =position_r.x +15;
	position_f.y =position_y.y +40;

	position_g.x =position_t.x +15;
	position_g.y =position_y.y +40;

	position_h.x =position_y.x +15;
	position_h.y =position_y.y +40;

	position_j.x =position_u.x +15;
	position_j.y =position_y.y +40;

	position_k.x =position_i.x +15;
	position_k.y =position_y.y +40;

	position_l.x =position_o.x +15;
	position_l.y =position_y.y +40;

	position_m.x =position_p.x +15;
	position_m.y =position_y.y +40;

	position_w.x =position_q.x +15;
	position_w.y =position_y.y +80;

	position_x.x =position_s.x +15;
	position_x.y =position_y.y +80;

	position_c.x =position_d.x +15;
	position_c.y =position_y.y +80;

	position_v.x =position_f.x +15;
	position_v.y =position_y.y +80;

	position_b.x =position_g.x +15;
	position_b.y =position_y.y +80;

	position_n.x =position_h.x +15;
	position_n.y =position_y.y +80;

	position_aa.x =position_j.x +15;
	position_aa.y =position_y.y +80;

	position_bb.x =position_k.x +15;
	position_bb.y =position_y.y +80;

	position_cc.x =position_l.x +15;
	position_cc.y =position_y.y +80;

	position_dd.x =position_m.x +15;
	position_dd.y =position_y.y +80;

	position_fenetre.x = (*maxW -350 ) /2;
	position_fenetre.y = *maxH /3.5;

	position_start.x = position_fenetre.x +75;
	position_start.y = position_fenetre.y + 130;

	position_nom.x = position_fenetre.x +110;
	position_nom.y = position_fenetre.y +90;

	position_back.x = 10;
	position_back.y = *maxH /1.1666;

	FILE* lecture_personnage = NULL;
    lecture_personnage = fopen("menu/personnage.txt", "r");
    if (lecture_personnage != NULL)
    {
    	fscanf(lecture_personnage,"%d",&choix_personnage);

    fclose(lecture_personnage); // On ferme le fichier qui a été ouvert

    }

    

	a = IMG_Load("menu/alphabet/a.png");
	b = IMG_Load("menu/alphabet/b.png");
	c = IMG_Load("menu/alphabet/c.png");
	d = IMG_Load("menu/alphabet/d.png");
	e = IMG_Load("menu/alphabet/e.png");
	f = IMG_Load("menu/alphabet/f.png");
	g = IMG_Load("menu/alphabet/g.png");
	h = IMG_Load("menu/alphabet/h.png");
	i = IMG_Load("menu/alphabet/i.png");
	j = IMG_Load("menu/alphabet/j.png");
	k = IMG_Load("menu/alphabet/k.png");
	l = IMG_Load("menu/alphabet/l.png");
	m = IMG_Load("menu/alphabet/m.png");
	n = IMG_Load("menu/alphabet/n.png");
	o = IMG_Load("menu/alphabet/o.png");
	p = IMG_Load("menu/alphabet/p.png");
	q = IMG_Load("menu/alphabet/q.png");
	r = IMG_Load("menu/alphabet/r.png");
	s = IMG_Load("menu/alphabet/s.png");
	t = IMG_Load("menu/alphabet/t.png");
	u = IMG_Load("menu/alphabet/u.png");
	v = IMG_Load("menu/alphabet/v.png");
	w = IMG_Load("menu/alphabet/w.png");
	x = IMG_Load("menu/alphabet/x.png");
	y = IMG_Load("menu/alphabet/y.png");
	z = IMG_Load("menu/alphabet/z.png");
	aa = IMG_Load("menu/alphabet/aa.png");
	bb = IMG_Load("menu/alphabet/bb.png");
	cc = IMG_Load("menu/alphabet/cc.png");
	dd = IMG_Load("menu/alphabet/cc.png");
	fenetre= IMG_Load("menu/saisie/saisie.png");
	start = IMG_Load("menu/saisie/start.png");
	startClic = IMG_Load("menu/saisie/startClic.png");
	back = IMG_Load("menu/choisir/back.png");
	backClic = IMG_Load("menu/choisir/backClic.png");


	FILE* fichier_nom = NULL;
    fichier_nom = fopen("menu/nom.txt", "a+");
    if (fichier_nom != NULL)
    {
    
       
        
     
	switch (event.type)
	{
		case SDL_MOUSEMOTION:
		if (event.motion.x>=position_back.x && event.motion.x <= position_back.x+330 && event.motion.y >= position_back.y && event.motion.y <= position_back.y+35 )
		{
			
    			etat_back=1;
		}
		else if (event.button.x>=position_start.x && event.button.x <= position_start.x+203 && event.button.y >= position_start.y && event.button.y <= position_start.y+53 )
		{
    			etat_start1=1;
		}
		break;

	
		case SDL_MOUSEBUTTONDOWN:
		if (event.button.x>=position_a.x && event.button.x <= position_a.x+33 && event.button.y >= position_a.y && event.button.y <= position_a.y+33 && var ==0)
		{
			var=1;
			fputc('A', fichier_nom);
    			
		}
		else if (event.button.x>=position_z.x && event.button.x <= position_z.x+33 && event.button.y >= position_z.y && event.button.y <= position_z.y+33 && var ==0)
		{
			var=1;
    		fputc('Z', fichier_nom);
    			
		}
		else if (event.button.x>=position_e.x && event.button.x <= position_e.x+33 && event.button.y >= position_e.y && event.button.y <= position_e.y+33 && var ==0)
		{
			var=1;
    		fputc('E', fichier_nom);
    			
		}
		else if (event.button.x>=position_r.x && event.button.x <= position_r.x+33 && event.button.y >= position_r.y && event.button.y <= position_r.y+33 && var ==0)
		{
			var=1;
    		fputc('R', fichier_nom);
    			
		}
		else if (event.button.x>=position_t.x && event.button.x <= position_t.x+33 && event.button.y >= position_t.y && event.button.y <= position_t.y+33 && var ==0)
		{
			var=1;
    		fputc('T', fichier_nom);
    			
		}
		else if (event.button.x>=position_y.x && event.button.x <= position_y.x+33 && event.button.y >= position_y.y && event.button.y <= position_y.y+33 && var ==0)
		{
			var=1;
    		fputc('Y', fichier_nom);
    			
		}
		else if (event.button.x>=position_u.x && event.button.x <= position_u.x+33 && event.button.y >= position_u.y && event.button.y <= position_u.y+33 && var ==0)
		{
			var=1;
    		fputc('U', fichier_nom);
    			
		}
		else if (event.button.x>=position_i.x && event.button.x <= position_i.x+33 && event.button.y >= position_i.y && event.button.y <= position_i.y+33 && var ==0)
		{
			var=1;
    		fputc('I', fichier_nom);
    			
		}
		else if (event.button.x>=position_o.x && event.button.x <= position_o.x+33 && event.button.y >= position_o.y && event.button.y <= position_o.y+33 && var ==0)
		{
			var=1;
    		fputc('O', fichier_nom);
    			
		}
		else if (event.button.x>=position_p.x && event.button.x <= position_p.x+33 && event.button.y >= position_p.y && event.button.y <= position_p.y+33 && var ==0)
		{
			var=1;
    		fputc('P', fichier_nom);
    			
		}
		else if (event.button.x>=position_q.x && event.button.x <= position_q.x+33 && event.button.y >= position_q.y && event.button.y <= position_q.y+33 && var ==0)
		{
			var=1;
			fputc('Q', fichier_nom);
    			
		}
		else if (event.button.x>=position_s.x && event.button.x <= position_s.x+33 && event.button.y >= position_s.y && event.button.y <= position_s.y+33 && var ==0)
		{
			var=1;
			fputc('S', fichier_nom);
    			
		}
		else if (event.button.x>=position_d.x && event.button.x <= position_d.x+33 && event.button.y >= position_d.y && event.button.y <= position_d.y+33 && var ==0)
		{
			var=1;
			fputc('D', fichier_nom);
    			
		}
		else if (event.button.x>=position_f.x && event.button.x <= position_f.x+33 && event.button.y >= position_f.y && event.button.y <= position_f.y+33 && var ==0)
		{
			var=1;
			fputc('F', fichier_nom);
    			
		}
		else if (event.button.x>=position_g.x && event.button.x <= position_g.x+33 && event.button.y >= position_g.y && event.button.y <= position_g.y+33 && var ==0)
		{
			var=1;
			fputc('G', fichier_nom);
    			
		}
		else if (event.button.x>=position_h.x && event.button.x <= position_h.x+33 && event.button.y >= position_h.y && event.button.y <= position_h.y+33 && var ==0)
		{
			var=1;
			fputc('H', fichier_nom);
    			
		}
		else if (event.button.x>=position_j.x && event.button.x <= position_j.x+33 && event.button.y >= position_j.y && event.button.y <= position_j.y+33 && var ==0)
		{
			var=1;
			fputc('J', fichier_nom);
    			
		}
		else if (event.button.x>=position_k.x && event.button.x <= position_k.x+33 && event.button.y >= position_k.y && event.button.y <= position_k.y+33 && var ==0)
		{
			var=1;
			fputc('K', fichier_nom);
    			
		}
		else if (event.button.x>=position_l.x && event.button.x <= position_l.x+33 && event.button.y >= position_l.y && event.button.y <= position_l.y+33 && var ==0)
		{
			var=1;
			fputc('L', fichier_nom);
    			
		}
		else if (event.button.x>=position_m.x && event.button.x <= position_m.x+33 && event.button.y >= position_m.y && event.button.y <= position_m.y+33 && var ==0)
		{
			var=1;
			fputc('M', fichier_nom);
    			
		}
		else if (event.button.x>=position_w.x && event.button.x <= position_w.x+33 && event.button.y >= position_w.y && event.button.y <= position_w.y+33 && var ==0)
		{
			var=1;
			fputc('W', fichier_nom);
    			
		}
		else if (event.button.x>=position_x.x && event.button.x <= position_x.x+33 && event.button.y >= position_x.y && event.button.y <= position_x.y+33 && var ==0)
		{
			var=1;
			fputc('X', fichier_nom);
    			
		}
		else if (event.button.x>=position_c.x && event.button.x <= position_c.x+33 && event.button.y >= position_c.y && event.button.y <= position_c.y+33 && var ==0)
		{
			var=1;
			fputc('C', fichier_nom);
    			
		}
		else if (event.button.x>=position_v.x && event.button.x <= position_v.x+33 && event.button.y >= position_v.y && event.button.y <= position_v.y+33 && var ==0)
		{
			var=1;
			fputc('V', fichier_nom);
    			
		}
		else if (event.button.x>=position_b.x && event.button.x <= position_b.x+33 && event.button.y >= position_b.y && event.button.y <= position_b.y+33 && var ==0)
		{
			var=1;
			fputc('B', fichier_nom);
    			
		}
		else if (event.button.x>=position_n.x && event.button.x <= position_n.x+33 && event.button.y >= position_n.y && event.button.y <= position_n.y+33 && var ==0)
		{
			var=1;
			fputc('N', fichier_nom);
    			
		}
		else if (event.button.x>=position_aa.x && event.button.x <= position_aa.x+33 && event.button.y >= position_aa.y && event.button.y <= position_aa.y+33 && var ==0)
		{
			var=1;
			fputc('?', fichier_nom);
    			
		}
		else if (event.button.x>=position_bb.x && event.button.x <= position_bb.x+33 && event.button.y >= position_bb.y && event.button.y <= position_bb.y+33 && var ==0)
		{
			var=1;
			fputc('!', fichier_nom);
    			
		}
		else if (event.button.x>=position_cc.x && event.button.x <= position_cc.x+33 && event.button.y >= position_cc.y && event.button.y <= position_cc.y+33 && var ==0)
		{
			var=1;
			fputc(':', fichier_nom);
    			
		}
		else if (event.button.x>=position_dd.x && event.button.x <= position_dd.x+33 && event.button.y >= position_dd.y && event.button.y <= position_dd.y+33 && var ==0)
		{
			var=1;
			fputc(':', fichier_nom);
    			
		}
		
		
		break;
		case SDL_MOUSEBUTTONUP:
		var = 0;
		if (event.button.x>=position_back.x && event.button.x <= position_back.x+330 && event.button.y >= position_back.y && event.button.y <= position_back.y+35 )
		{
			
    			*pointeur = 0;
    			SDL_WarpMouse(event.motion.x+1, event.motion.y+1);
    			etat_start=0;
    			remove("menu/nom.txt");
    		
		}
		else if (event.button.x>=position_start.x && event.button.x <= position_start.x+203 && event.button.y >= position_start.y && event.button.y <= position_start.y+53 )
		{
    			etat_start=1;
		}
		break;
		
	
    }
    fclose(fichier_nom); // On ferme le fichier qui a été ouvert

    }

if (etat_back == 1)
	{
		SDL_BlitSurface(backClic, NULL , ecran, &position_back);
		
    			
	}
	if (etat_start == 1 )
	{
		SDL_BlitSurface(startClic, NULL , ecran, &position_start);
		SDL_BlitSurface(startClic, NULL , ecran, &position_start);
		return 0;

		/*ici fonction du commencement jeu */ 
				
	}
	if ( etat_start1==1) /*temporaire*/
	{
		SDL_BlitSurface(startClic, NULL , ecran, &position_start);
		SDL_BlitSurface(startClic, NULL , ecran, &position_start);

				
	}
      
	
	SDL_BlitSurface(a, NULL , ecran, &position_a);
	SDL_BlitSurface(z, NULL , ecran, &position_z);
	SDL_BlitSurface(e, NULL , ecran, &position_e);
	SDL_BlitSurface(r, NULL , ecran, &position_r);
	SDL_BlitSurface(t, NULL , ecran, &position_t);
	SDL_BlitSurface(y, NULL , ecran, &position_y);
	SDL_BlitSurface(u, NULL , ecran, &position_u);
	SDL_BlitSurface(i, NULL , ecran, &position_i);
	SDL_BlitSurface(o, NULL , ecran, &position_o);
	SDL_BlitSurface(p, NULL , ecran, &position_p);
	SDL_BlitSurface(q, NULL , ecran, &position_q);
	SDL_BlitSurface(s, NULL , ecran, &position_s);
	SDL_BlitSurface(d, NULL , ecran, &position_d);
	SDL_BlitSurface(f, NULL , ecran, &position_f);
	SDL_BlitSurface(g, NULL , ecran, &position_g);
	SDL_BlitSurface(h, NULL , ecran, &position_h);
	SDL_BlitSurface(j, NULL , ecran, &position_j);
	SDL_BlitSurface(k, NULL , ecran, &position_k);
	SDL_BlitSurface(l, NULL , ecran, &position_l);
	SDL_BlitSurface(m, NULL , ecran, &position_m);
	SDL_BlitSurface(w, NULL , ecran, &position_w);
	SDL_BlitSurface(x, NULL , ecran, &position_x);
	SDL_BlitSurface(c, NULL , ecran, &position_c);
	SDL_BlitSurface(v, NULL , ecran, &position_v);
	SDL_BlitSurface(b, NULL , ecran, &position_b);
	SDL_BlitSurface(n, NULL , ecran, &position_n);
	SDL_BlitSurface(aa, NULL , ecran, &position_aa);
	SDL_BlitSurface(bb, NULL , ecran, &position_bb);
	SDL_BlitSurface(cc, NULL , ecran, &position_cc);
	SDL_BlitSurface(dd, NULL , ecran, &position_dd);
	SDL_BlitSurface(fenetre, NULL, ecran , &position_fenetre);
	
	if (etat_back==0)
	{
		SDL_BlitSurface(back, NULL, ecran, &position_back);
	}
	if (etat_start==0&& etat_start1!=1)
	{
		SDL_BlitSurface(start, NULL, ecran, &position_start);
	}
	
	
	
		
		
    


	TTF_Init();
    if(TTF_Init() == -1)
	{
    fprintf(stderr, "Erreur d'initialisation de TTF_Init : %s\n", TTF_GetError());
    exit(EXIT_FAILURE);
	}
	SDL_Color couleurNoire = {0, 0, 0};
    TTF_Font *police = NULL;
    police = TTF_OpenFont("menu/fonts/ComicPerso.ttf", 30);
    

    
    FILE* lecture_nom = NULL;
    lecture_nom = fopen("menu/nom.txt", "r");
    if (lecture_nom != NULL)
    {
    	fgets(lettre, 10, lecture_nom);
    	longeur =  strlen(lettre);
    	if (longeur > 0)
    	{
    		nom = TTF_RenderText_Blended(police, lettre, couleurNoire);
    	}
        
    SDL_BlitSurface (nom, NULL, ecran, &position_nom);

    fclose(lecture_nom); // On ferme le fichier qui a été ouvert

    }

    FILE* nom_final = NULL;
    nom_final = fopen("menu/nom_final.txt", "w+");
    if (lecture_nom != NULL)
    {
    	fputs(lettre, nom_final);
    }
    fclose(nom_final);

    FILE* enregistre_personnage = NULL;
    enregistre_personnage = fopen("menu/nom_final.txt", "a+");
    if (enregistre_personnage != NULL)
    {
    	fprintf(enregistre_personnage,"  %d",choix_personnage);

    fclose(enregistre_personnage); // On ferme le fichier qui a été ouvert

    }
    	
    
   

     
	SDL_FreeSurface(start);
	SDL_FreeSurface(startClic);
	SDL_FreeSurface(fenetre);
	SDL_FreeSurface(dd);
	SDL_FreeSurface(cc);
	SDL_FreeSurface(bb);
	SDL_FreeSurface(aa);
	SDL_FreeSurface(n);
	SDL_FreeSurface(b);
	SDL_FreeSurface(v);
	SDL_FreeSurface(c);
	SDL_FreeSurface(x);
	SDL_FreeSurface(w);
	SDL_FreeSurface(m);
	SDL_FreeSurface(l);
	SDL_FreeSurface(k);
	SDL_FreeSurface(j);
	SDL_FreeSurface(h);
	SDL_FreeSurface(g);
	SDL_FreeSurface(f);
	SDL_FreeSurface(d);
	SDL_FreeSurface(s);
	SDL_FreeSurface(q);
	SDL_FreeSurface(p);
	SDL_FreeSurface(o);
	SDL_FreeSurface(i);
	SDL_FreeSurface(u);
	SDL_FreeSurface(y);
	SDL_FreeSurface(t);
	SDL_FreeSurface(r);
	SDL_FreeSurface(e);
	SDL_FreeSurface(z);
	SDL_FreeSurface(a);
	SDL_FreeSurface(back);
	SDL_FreeSurface(backClic);
	SDL_FreeSurface(nom);
	TTF_CloseFont(police);


}